{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=2)\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "from collections import Counter\n",
    "\n",
    "sns.set_style('ticks')\n",
    "\n",
    "%matplotlib inline\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import matplotlib as mpl\n",
    "mpl.rcParams['figure.dpi']= 300\n",
    "mpl.rc(\"savefig\", dpi=300)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Read files and select drugs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# log2_median_ic50, log2_median_ic50_9f, log2_median_ic50_hn, log2_median_ic50_3f_hn, log2_median_ic50_9f_hn, log2_max_conc, log2_median_ic50_3f_hn\n",
    "ref_type = 'log2_median_ic50_hn' # log2_median_ic50_3f_hn | log2_median_ic50_hn\n",
    "model_name = 'hn_drug_cw_dw10_100000_model'\n",
    "\n",
    "# for each patient, if cell cluster is less than 5%, then we don't consider that cluster \n",
    "freq_cutoff = 0.05\n",
    "\n",
    "# shift the dosage as GDSC experiment (Syto60) is less sensitive\n",
    "dosage_shifted = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(81, 27)\n"
     ]
    }
   ],
   "source": [
    "drug_info_df = pd.read_csv('../preprocessed_data/GDSC/hn_drug_stat.csv', index_col=0)\n",
    "drug_info_df.index = drug_info_df.index.astype(str)\n",
    "\n",
    "drug_id_name_dict = dict(zip(drug_info_df.index, drug_info_df['Drug Name']))\n",
    "print (drug_info_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tested_drug_list = [1032, 1007, 133, 201, 1010] + [182, 301, 302]\n",
    "[d for d in tested_drug_list if d not in drug_info_df.index.astype(int)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Read predicted IC50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "norm_type = 'patient_TPM'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "cadrres_patient_df = pd.read_csv('../result/HN_model/{}/pred_gdsc_no_bias_{}.csv'.format(norm_type, model_name), index_col=0)\n",
    "out_dir = '../result/HN_model/{}/'.format(norm_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1001</th>\n",
       "      <th>1003</th>\n",
       "      <th>1004</th>\n",
       "      <th>1006</th>\n",
       "      <th>1007</th>\n",
       "      <th>1010</th>\n",
       "      <th>1012</th>\n",
       "      <th>1014</th>\n",
       "      <th>1015</th>\n",
       "      <th>1016</th>\n",
       "      <th>...</th>\n",
       "      <th>299</th>\n",
       "      <th>301</th>\n",
       "      <th>302</th>\n",
       "      <th>303</th>\n",
       "      <th>305</th>\n",
       "      <th>306</th>\n",
       "      <th>308</th>\n",
       "      <th>328</th>\n",
       "      <th>331</th>\n",
       "      <th>346</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HN120</th>\n",
       "      <td>10.582092</td>\n",
       "      <td>-6.993030</td>\n",
       "      <td>-6.289053</td>\n",
       "      <td>-0.678505</td>\n",
       "      <td>-6.666853</td>\n",
       "      <td>0.953735</td>\n",
       "      <td>0.099626</td>\n",
       "      <td>1.763727</td>\n",
       "      <td>2.467532</td>\n",
       "      <td>-1.884118</td>\n",
       "      <td>...</td>\n",
       "      <td>0.199231</td>\n",
       "      <td>2.577249</td>\n",
       "      <td>0.565079</td>\n",
       "      <td>2.664446</td>\n",
       "      <td>3.350329</td>\n",
       "      <td>2.539095</td>\n",
       "      <td>-1.974207</td>\n",
       "      <td>-2.879822</td>\n",
       "      <td>1.058784</td>\n",
       "      <td>-5.215218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN137</th>\n",
       "      <td>10.342202</td>\n",
       "      <td>-7.301405</td>\n",
       "      <td>-7.577541</td>\n",
       "      <td>-1.305941</td>\n",
       "      <td>-8.006421</td>\n",
       "      <td>0.501566</td>\n",
       "      <td>0.019054</td>\n",
       "      <td>1.248602</td>\n",
       "      <td>2.418054</td>\n",
       "      <td>-1.999318</td>\n",
       "      <td>...</td>\n",
       "      <td>1.659174</td>\n",
       "      <td>2.934579</td>\n",
       "      <td>1.547792</td>\n",
       "      <td>3.141412</td>\n",
       "      <td>3.411621</td>\n",
       "      <td>2.536889</td>\n",
       "      <td>-2.161168</td>\n",
       "      <td>-3.057182</td>\n",
       "      <td>1.876328</td>\n",
       "      <td>-4.953847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN148</th>\n",
       "      <td>10.686618</td>\n",
       "      <td>-5.308124</td>\n",
       "      <td>-4.918613</td>\n",
       "      <td>0.726391</td>\n",
       "      <td>-6.392437</td>\n",
       "      <td>-0.025692</td>\n",
       "      <td>0.962065</td>\n",
       "      <td>1.008880</td>\n",
       "      <td>2.257967</td>\n",
       "      <td>-1.227807</td>\n",
       "      <td>...</td>\n",
       "      <td>2.837512</td>\n",
       "      <td>3.486805</td>\n",
       "      <td>2.487200</td>\n",
       "      <td>4.079376</td>\n",
       "      <td>4.380871</td>\n",
       "      <td>3.243483</td>\n",
       "      <td>-0.153279</td>\n",
       "      <td>-0.816008</td>\n",
       "      <td>2.892805</td>\n",
       "      <td>-2.348328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN159</th>\n",
       "      <td>11.391786</td>\n",
       "      <td>-5.094658</td>\n",
       "      <td>-5.172881</td>\n",
       "      <td>1.001772</td>\n",
       "      <td>-6.563778</td>\n",
       "      <td>1.594693</td>\n",
       "      <td>2.003104</td>\n",
       "      <td>3.031630</td>\n",
       "      <td>4.057887</td>\n",
       "      <td>-0.314321</td>\n",
       "      <td>...</td>\n",
       "      <td>0.287670</td>\n",
       "      <td>1.870677</td>\n",
       "      <td>0.331834</td>\n",
       "      <td>2.989429</td>\n",
       "      <td>3.922742</td>\n",
       "      <td>2.686959</td>\n",
       "      <td>0.606889</td>\n",
       "      <td>-1.700831</td>\n",
       "      <td>0.812231</td>\n",
       "      <td>-4.132381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN160</th>\n",
       "      <td>10.757305</td>\n",
       "      <td>-5.201873</td>\n",
       "      <td>-6.008071</td>\n",
       "      <td>1.281516</td>\n",
       "      <td>-6.505566</td>\n",
       "      <td>1.036287</td>\n",
       "      <td>1.609897</td>\n",
       "      <td>3.500886</td>\n",
       "      <td>4.339465</td>\n",
       "      <td>-2.149608</td>\n",
       "      <td>...</td>\n",
       "      <td>0.018260</td>\n",
       "      <td>2.102799</td>\n",
       "      <td>-0.371211</td>\n",
       "      <td>3.355045</td>\n",
       "      <td>3.796809</td>\n",
       "      <td>2.410083</td>\n",
       "      <td>1.121997</td>\n",
       "      <td>-0.320819</td>\n",
       "      <td>1.262494</td>\n",
       "      <td>-2.120933</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 81 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            1001      1003      1004      1006      1007      1010      1012  \\\n",
       "HN120  10.582092 -6.993030 -6.289053 -0.678505 -6.666853  0.953735  0.099626   \n",
       "HN137  10.342202 -7.301405 -7.577541 -1.305941 -8.006421  0.501566  0.019054   \n",
       "HN148  10.686618 -5.308124 -4.918613  0.726391 -6.392437 -0.025692  0.962065   \n",
       "HN159  11.391786 -5.094658 -5.172881  1.001772 -6.563778  1.594693  2.003104   \n",
       "HN160  10.757305 -5.201873 -6.008071  1.281516 -6.505566  1.036287  1.609897   \n",
       "\n",
       "           1014      1015      1016  ...       299       301       302  \\\n",
       "HN120  1.763727  2.467532 -1.884118  ...  0.199231  2.577249  0.565079   \n",
       "HN137  1.248602  2.418054 -1.999318  ...  1.659174  2.934579  1.547792   \n",
       "HN148  1.008880  2.257967 -1.227807  ...  2.837512  3.486805  2.487200   \n",
       "HN159  3.031630  4.057887 -0.314321  ...  0.287670  1.870677  0.331834   \n",
       "HN160  3.500886  4.339465 -2.149608  ...  0.018260  2.102799 -0.371211   \n",
       "\n",
       "            303       305       306       308       328       331       346  \n",
       "HN120  2.664446  3.350329  2.539095 -1.974207 -2.879822  1.058784 -5.215218  \n",
       "HN137  3.141412  3.411621  2.536889 -2.161168 -3.057182  1.876328 -4.953847  \n",
       "HN148  4.079376  4.380871  3.243483 -0.153279 -0.816008  2.892805 -2.348328  \n",
       "HN159  2.989429  3.922742  2.686959  0.606889 -1.700831  0.812231 -4.132381  \n",
       "HN160  3.355045  3.796809  2.410083  1.121997 -0.320819  1.262494 -2.120933  \n",
       "\n",
       "[5 rows x 81 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cadrres_patient_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81 6\n"
     ]
    }
   ],
   "source": [
    "drug_list = drug_info_df.index\n",
    "patient_list = cadrres_patient_df.index\n",
    "print(len(drug_list), len(patient_list))\n",
    "\n",
    "drug_info_df = drug_info_df.loc[drug_list]\n",
    "cadrres_patient_df = cadrres_patient_df[drug_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "if dosage_shifted:\n",
    "    # Shift by 4 uM\n",
    "    cadrres_patient_df = cadrres_patient_df - 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### List all pairs of patient and drug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Drug ID</th>\n",
       "      <th>1001</th>\n",
       "      <th>1003</th>\n",
       "      <th>1004</th>\n",
       "      <th>1006</th>\n",
       "      <th>1007</th>\n",
       "      <th>1010</th>\n",
       "      <th>1012</th>\n",
       "      <th>1014</th>\n",
       "      <th>1015</th>\n",
       "      <th>1016</th>\n",
       "      <th>...</th>\n",
       "      <th>299</th>\n",
       "      <th>301</th>\n",
       "      <th>302</th>\n",
       "      <th>303</th>\n",
       "      <th>305</th>\n",
       "      <th>306</th>\n",
       "      <th>308</th>\n",
       "      <th>328</th>\n",
       "      <th>331</th>\n",
       "      <th>346</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HN120</th>\n",
       "      <td>39.049925</td>\n",
       "      <td>39.842087</td>\n",
       "      <td>35.492878</td>\n",
       "      <td>38.690576</td>\n",
       "      <td>10.276450</td>\n",
       "      <td>16.601540</td>\n",
       "      <td>60.440672</td>\n",
       "      <td>33.595293</td>\n",
       "      <td>49.593540</td>\n",
       "      <td>49.670575</td>\n",
       "      <td>...</td>\n",
       "      <td>97.186756</td>\n",
       "      <td>67.116298</td>\n",
       "      <td>84.901506</td>\n",
       "      <td>79.616156</td>\n",
       "      <td>77.146049</td>\n",
       "      <td>62.379669</td>\n",
       "      <td>81.139437</td>\n",
       "      <td>78.550861</td>\n",
       "      <td>78.600930</td>\n",
       "      <td>89.517887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN137</th>\n",
       "      <td>43.071491</td>\n",
       "      <td>45.058686</td>\n",
       "      <td>57.338329</td>\n",
       "      <td>49.364222</td>\n",
       "      <td>22.472146</td>\n",
       "      <td>21.404472</td>\n",
       "      <td>61.767905</td>\n",
       "      <td>41.962207</td>\n",
       "      <td>50.450896</td>\n",
       "      <td>51.666222</td>\n",
       "      <td>...</td>\n",
       "      <td>92.624159</td>\n",
       "      <td>61.438141</td>\n",
       "      <td>73.995441</td>\n",
       "      <td>73.727766</td>\n",
       "      <td>76.388385</td>\n",
       "      <td>62.415545</td>\n",
       "      <td>83.043140</td>\n",
       "      <td>80.549491</td>\n",
       "      <td>67.576091</td>\n",
       "      <td>87.692203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN148</th>\n",
       "      <td>37.339828</td>\n",
       "      <td>17.080440</td>\n",
       "      <td>17.546785</td>\n",
       "      <td>19.245484</td>\n",
       "      <td>8.650394</td>\n",
       "      <td>28.186133</td>\n",
       "      <td>45.662509</td>\n",
       "      <td>46.054172</td>\n",
       "      <td>53.220541</td>\n",
       "      <td>38.506675</td>\n",
       "      <td>...</td>\n",
       "      <td>84.729989</td>\n",
       "      <td>52.073647</td>\n",
       "      <td>59.738570</td>\n",
       "      <td>59.428795</td>\n",
       "      <td>62.299004</td>\n",
       "      <td>50.436243</td>\n",
       "      <td>54.907273</td>\n",
       "      <td>46.693210</td>\n",
       "      <td>50.744693</td>\n",
       "      <td>53.931666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN159</th>\n",
       "      <td>26.767503</td>\n",
       "      <td>15.085639</td>\n",
       "      <td>20.243978</td>\n",
       "      <td>16.451410</td>\n",
       "      <td>9.636134</td>\n",
       "      <td>11.320510</td>\n",
       "      <td>28.996999</td>\n",
       "      <td>17.361429</td>\n",
       "      <td>24.626937</td>\n",
       "      <td>24.950061</td>\n",
       "      <td>...</td>\n",
       "      <td>97.014215</td>\n",
       "      <td>76.909501</td>\n",
       "      <td>86.859241</td>\n",
       "      <td>75.716769</td>\n",
       "      <td>69.419594</td>\n",
       "      <td>59.945694</td>\n",
       "      <td>41.824479</td>\n",
       "      <td>61.794804</td>\n",
       "      <td>81.335118</td>\n",
       "      <td>80.126241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN160</th>\n",
       "      <td>36.200743</td>\n",
       "      <td>16.062505</td>\n",
       "      <td>31.169527</td>\n",
       "      <td>13.956344</td>\n",
       "      <td>9.290461</td>\n",
       "      <td>15.824357</td>\n",
       "      <td>34.910416</td>\n",
       "      <td>13.175963</td>\n",
       "      <td>21.185432</td>\n",
       "      <td>54.260814</td>\n",
       "      <td>...</td>\n",
       "      <td>97.510153</td>\n",
       "      <td>73.929604</td>\n",
       "      <td>91.496969</td>\n",
       "      <td>70.760664</td>\n",
       "      <td>71.240627</td>\n",
       "      <td>64.453888</td>\n",
       "      <td>33.469443</td>\n",
       "      <td>38.326720</td>\n",
       "      <td>76.130277</td>\n",
       "      <td>49.999347</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 81 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Drug ID       1001       1003       1004       1006       1007       1010  \\\n",
       "HN120    39.049925  39.842087  35.492878  38.690576  10.276450  16.601540   \n",
       "HN137    43.071491  45.058686  57.338329  49.364222  22.472146  21.404472   \n",
       "HN148    37.339828  17.080440  17.546785  19.245484   8.650394  28.186133   \n",
       "HN159    26.767503  15.085639  20.243978  16.451410   9.636134  11.320510   \n",
       "HN160    36.200743  16.062505  31.169527  13.956344   9.290461  15.824357   \n",
       "\n",
       "Drug ID       1012       1014       1015       1016  ...        299  \\\n",
       "HN120    60.440672  33.595293  49.593540  49.670575  ...  97.186756   \n",
       "HN137    61.767905  41.962207  50.450896  51.666222  ...  92.624159   \n",
       "HN148    45.662509  46.054172  53.220541  38.506675  ...  84.729989   \n",
       "HN159    28.996999  17.361429  24.626937  24.950061  ...  97.014215   \n",
       "HN160    34.910416  13.175963  21.185432  54.260814  ...  97.510153   \n",
       "\n",
       "Drug ID        301        302        303        305        306        308  \\\n",
       "HN120    67.116298  84.901506  79.616156  77.146049  62.379669  81.139437   \n",
       "HN137    61.438141  73.995441  73.727766  76.388385  62.415545  83.043140   \n",
       "HN148    52.073647  59.738570  59.428795  62.299004  50.436243  54.907273   \n",
       "HN159    76.909501  86.859241  75.716769  69.419594  59.945694  41.824479   \n",
       "HN160    73.929604  91.496969  70.760664  71.240627  64.453888  33.469443   \n",
       "\n",
       "Drug ID        328        331        346  \n",
       "HN120    78.550861  78.600930  89.517887  \n",
       "HN137    80.549491  67.576091  87.692203  \n",
       "HN148    46.693210  50.744693  53.931666  \n",
       "HN159    61.794804  81.335118  80.126241  \n",
       "HN160    38.326720  76.130277  49.999347  \n",
       "\n",
       "[5 rows x 81 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred_delta_df = pd.DataFrame(cadrres_patient_df.values - drug_info_df[ref_type].values, columns=drug_list, index=patient_list)\n",
    "pred_cv_df = 100 / (1 + (np.power(2, -pred_delta_df)))\n",
    "pred_kill_df = 100 - pred_cv_df\n",
    "\n",
    "pred_kill_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id</th>\n",
       "      <th>kill</th>\n",
       "      <th>drug_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1001</td>\n",
       "      <td>39.049925</td>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1003</td>\n",
       "      <td>39.842087</td>\n",
       "      <td>Camptothecin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1004</td>\n",
       "      <td>35.492878</td>\n",
       "      <td>Vinblastine</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1006</td>\n",
       "      <td>38.690576</td>\n",
       "      <td>Cytarabine</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>10.276450</td>\n",
       "      <td>Docetaxel</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id       kill            drug_name\n",
       "0   HN120    1001  39.049925  AICA Ribonucleotide\n",
       "1   HN120    1003  39.842087         Camptothecin\n",
       "2   HN120    1004  35.492878          Vinblastine\n",
       "3   HN120    1006  38.690576           Cytarabine\n",
       "4   HN120    1007  10.276450            Docetaxel"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_drug_pred_df = pred_kill_df.stack().reset_index()\n",
    "single_drug_pred_df.columns = ['patient', 'drug_id', 'kill']\n",
    "single_drug_pred_df.loc[:, 'drug_name'] = [drug_id_name_dict[d] for d in single_drug_pred_df['drug_id'].values]\n",
    "single_drug_pred_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Save results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "if dosage_shifted:\n",
    "    single_drug_pred_df.to_csv(out_dir + 'pred_drug_kill_{}_{}_shifted.csv'.format(ref_type, model_name), index=False)\n",
    "else:\n",
    "    single_drug_pred_df.to_csv(out_dir + 'pred_drug_kill_{}_{}.csv'.format(ref_type, model_name), index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
